package com.acerola.imageconvert.controller;

import com.acerola.imageconvert.service.ImageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: ImageConvert
 * @description:
 * @author: Acerola
 * @create: 2022-02-17 22:17
 **/
@RestController
public class Convert {
    @Autowired
    private ImageConverter imageConverter;
    @Value("${number}")
    private int number;

    @RequestMapping("/convert")
    public String imgconvert(String path, String url) throws Exception {
        System.out.println(url);
        String fileType = path.substring(path.lastIndexOf(".") + 1, path.length()).toLowerCase();

        if (fileType.equals("png")) {
            String jpg = imageConverter.convert(fileType, "jpg", path);
            System.out.println(jpg);
            String id = jpg.substring(number, (jpg.toLowerCase().lastIndexOf("jpg".toLowerCase().trim())) - 1);
            String finalPath = url + id;
            System.out.println(finalPath);
            return finalPath;
        }
//        if (fileType.equals("jpg") || fileType.equals("jpeg") || fileType.equals("jfif"))
        else {
            String png = imageConverter.convert(fileType, "png", path);
            System.out.println(png);
            String id = png.substring(number, (png.toLowerCase().lastIndexOf("png".toLowerCase().trim())) - 1);
            String finalPath = url + id;
            System.out.println(finalPath);
            return finalPath;
        }
    }

}
